#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>


using namespace std;

typedef long long ll;


int smartSum(int number, vector<int>& si, vector<int>& fi) {
    const int INF = 0x3f3f3f3f;
    const int OFFSET = 1000 * 100;
    const int MAXS = 2000 * 100;
    vector<int> dp(MAXS + 1, -INF); // dp[s的和] = 能达到的最大的f的和
    dp[OFFSET] = 0;
    for (int i = 0;i < si.size();i++) {
        if (si[i] > 0) { // 对于大于零的
            for (int j = MAXS;j >= 0;j--) { // 从大到小转移
                if (dp[j] == -INF)continue;
                if (j + si[i] > MAXS)continue;
                dp[j + si[i]] = max(dp[j + si[i]], dp[j] + fi[i]);
            }
        }
        else if (si[i] < 0) { // 对于小于零的
            for (int j = 0;j <= MAXS;j++) { // 从小到大转移
                if (dp[j] == -INF)continue;
                if (j + si[i] < 0)continue;
                dp[j + si[i]] = max(dp[j + si[i]], dp[j] + fi[i]);
            }
        }
        else if (fi[i] > 0) { // si[i] == 0 等于零的
            for (int j = MAXS;j >= 0;j--) {
                if (dp[j] == -INF)continue;
                dp[j] += fi[i]; // 自增
            }
        }
    }
    int ans = 0;
    for (int i = OFFSET;i <= MAXS;i++) { // s 的和 非负的范围内找
        if (dp[i] < 0)continue;
        ans = max(ans, i + dp[i] - OFFSET);// 最大的 s的和 加 f的和
    }
    return ans;
}

int main() {
    int n;
    vector<int> a, b;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int u, v;
        cin >> u >> v;
        a.push_back(u);
        b.push_back(v);
    }

    cout << smartSum(n, a, b) << endl;


    return 0;
}